#pragma once

#include "Misc/CommonMacros.h"
#include "Containers/Delegate.h"
#include "Containers/Map.h"
#include "IPAddress.h"
#include "Containers/ThreadSafePtr.h"

class CTCPServer;
class CTCPClient;
class CHttpIncomingRequest;
class CHttpResponse;

class WHENGINE_API CHttpServer
{
public:
    CHttpServer(const SIPAddress& InIP, uint16_t InPort);
    ~CHttpServer();

    void AddRoute(const CString& InAbsPath
        ,std::function<std::shared_ptr<CHttpResponse>(std::shared_ptr<CHttpIncomingRequest>)> InFunction);

protected:
    TThreadSafeSharedPtr<CTCPServer> Server;
    std::shared_ptr<CHttpResponse> RouteRequest(std::shared_ptr<CHttpIncomingRequest> Request);

    TMap<CString /*abs path*/, std::function<std::shared_ptr<CHttpResponse>(std::shared_ptr<CHttpIncomingRequest>)>> Routes;

    void AddClient(TThreadSafeSharedPtr<CTCPClient> Client);
    TVector<TThreadSafeSharedPtr<CTCPClient>> Clients;

};